package com.hd.trans.framework.dialog;

import android.app.Dialog;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hd.trans.R;
import com.hd.trans.framework.languages.ShareItemBean;
import com.hd.trans.framework.languages.ShareResultAdapter;

import java.util.ArrayList;

public class ShareDialog extends DialogFragment {
    private boolean isNation;
    private ShareDialogModule module;
    private RecyclerView recycler;
    private ShareResultAdapter adapter;
    private TextView cancel;
    private ArrayList<ShareItemBean> item;

    public ShareDialog() {

    }

    public ShareDialog(ShareDialogModule module, boolean isNation) {
        this.module = module;
        this.isNation = isNation;
    }

    protected void setShareDialog(ShareDialogModule module) {
        this.module = module;
    }

    public static Builder builder() {
        return new Builder();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置dialog样式
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BottomDialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(isNation?R.layout.dialog_share_layout2:R.layout.dialog_share_layout, null);
        initDialog();
        initView(view);
        return view;
    }

    private void initDialog() {
        Dialog dialog = getDialog();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景为透明
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    private void initView(View view) {
        recycler = view.findViewById(R.id.recycler);
        cancel = view.findViewById(R.id.cancel);

        if(isNation)module.getList().clear();

        if (module.getList() == null || module.getList().size() < 1) {
            if (item == null) {
                item = new ArrayList();
            } else {
                item.clear();
            }
            item.add(new ShareItemBean(isNation?R.mipmap.text_icon_share_word2:R.mipmap.text_icon_share_word, getResources().getString(R.string.export_word)));
            item.add(new ShareItemBean(isNation?R.mipmap.text_icon_share_txt2:R.mipmap.text_icon_share_txt, getResources().getString(R.string.export_txt)));
            item.add(new ShareItemBean(isNation?R.mipmap.text_icon_share_pdf2:R.mipmap.text_icon_share_pdf, getResources().getString(R.string.export_pdf)));
            module.setList(item);
        }

        if(isNation){
            recycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        }else {
            recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        }
        adapter = new ShareResultAdapter(getDialog(), module, isNation);
        recycler.setAdapter(adapter);

        //主题色
        if (module.isButtonColor()) {
            cancel.setText(module.getColor());
            cancel.setBackgroundTintList(ColorStateList.valueOf(module.getColor()));
        }
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().cancel();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        //可以在createView里一次写
        Dialog dialog = getDialog();
        //设置dialog 宽高
        if (dialog != null) {
            int width = Resources.getSystem().getDisplayMetrics().widthPixels;
            dialog.getWindow().setLayout((int) width, LinearLayout.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setGravity(Gravity.BOTTOM);
        }
    }


    public static class Builder {

        private ShareDialogModule module;

        Builder() {
            module = new ShareDialogModule();
        }

        public ShareDialog build(boolean isNation) {
            return new ShareDialog(module,isNation);
        }

        public Builder setList(ArrayList<ShareItemBean> list) {
            if (null != list) module.setList(list);
            return this;
        }

        public Builder addItem(ShareItemBean item) {
            module.addItem(item);
            return this;
        }

        public Builder setButtonColor(int color) {
            module.setColor(color);
            return this;
        }

        public Builder setItemClickListenner(OnShareItemClickListenner listenner) {
            module.setListenner(listenner);
            return this;
        }

        public Builder setButtonColor(String color) {
            if (isColor(color)) {
                module.setColor(Color.parseColor(color));
                module.setButtonColor(true);
            } else {
                module.setButtonColor(false);
            }
            return this;
        }

        private boolean isColor(String color) {
            return !TextUtils.isEmpty(color) && color.matches("^#([a-fA-F\\d]{6}|[a-fA-F\\d]{3}|[a-fA-F\\d]{8})$");
        }

    }

    public interface OnShareItemClickListenner {
        void OnItemClick(Dialog dialog, ShareResultAdapter adapter, int position);
    }
}
